from jrcsignal.models import JrcSignal
from jrcmodel.models import CST
import datetime
from decimal import Decimal

class SignalsOfYear:
    cst_id = 0
    year = 0
    monthes = []
    
    def __init__(self, cst_id, year):
        self.cst_id = int(cst_id)
        self.year = year
        self.monthes = [SignalsOfMonth(self.cst_id, year, 1), SignalsOfMonth(self.cst_id, year, 2), SignalsOfMonth(self.cst_id, year, 3)
                        , SignalsOfMonth(self.cst_id, year, 4), SignalsOfMonth(self.cst_id, year, 5), SignalsOfMonth(self.cst_id, year, 6)
                        , SignalsOfMonth(self.cst_id, year, 7), SignalsOfMonth(self.cst_id, year, 8), SignalsOfMonth(self.cst_id, year, 9)
                        , SignalsOfMonth(self.cst_id, year, 10), SignalsOfMonth(self.cst_id, year, 11), SignalsOfMonth(self.cst_id, year, 12)]
        
    def __str__(self):
        return "Year %d, containing 12 months w/ signals" % self.year

class SignalsOfMonth:
    cst_id = 0
    year = 0
    month = 0
    _signals = None
    _cst = None
    _first_signal = None

    def __init__(self, cst_id, year, month):
        self.cst_id = int(cst_id)
        self.year = year
        self.month = month
        self._first_signal = None
        
    def get_cst(self):
        print "Getting CST #%d" % self.cst_id
        if self._cst == None:
            self._cst = CST.objects.get(pk = self.cst_id)
        return self._cst

    def is_date_after_first_signal(self):
        """ Returns True if Signal date is after first Signal date """
#        print "self._first_signal: %s" % self._first_signal

        if self._first_signal is None:
            signals = JrcSignal.objects.filter(cst_id = self.cst_id).order_by("date_first")[:1]
            if signals is not None and signals > 0:
                self._first_signal = signals[0]
            else:
                self._first_signal = JrcSignal()
                self._first_signal.date_first = datetime.datetime.strptime("2008-01", "%Y-%m")

        # Simplest case - year is greater
        if self.year > self._first_signal.date_first.year:
            return True
        # Year is the same, but month is later
        if self.year == self._first_signal.date_first.year and self.month >= self._first_signal.date_first.month:
            return True
        return False

    def is_in_future(self):
        """ Returns True if Signals are in future """
        dt = datetime.datetime.now()
        if self.year >= dt.year and self.month > dt.month:
            return True
        return False
    
    def is_forex(self):
        """ Returns True if CST to which signals are belong is for Forex asset class """
        cst = self.get_cst()
#        print "Asset class: %s" % cst.market_direction_1.market_id.asset.name
        if cst.market_direction_1.market_id.asset.name == "Forex":
            return True
        return False

    def get_signals(self):
        if self._signals == None:
            start = "%d-%d-01" % (self.year, self.month)
            if self.month == 12:
                end = "%d-01-01" % (self.year + 1)
            else:
                end = "%d-%d-01" % (self.year, self.month + 1)
#            print "Searching for signals (%s - %s)" % (start, end)
            self._signals = JrcSignal.objects.filter(cst_id = self.cst_id).filter(date_first__gte = start).filter(date_first__lt = end)

        return self._signals
    
    def get_aggregated_pips(self):
        """ Returns array of aggregated pips for current month """
        if len(self.get_signals()) < 1:
            return []
        v = Decimal(0)
        ar = [0] * len(self.get_signals())
        pos = 0
        for s in self.get_signals():
            v = v + s.get_pips()
            ar[pos] = v
            pos += 1
        return ar
    
    def get_signals_div_height(self):
        """ Returns height in px for signals """
        if self.get_signals_qnt() > 0:
            return self.get_signals_qnt() * 20
        return 40

    def get_signals_qnt(self):
        l = 0
        if self._signals != None:
            l = len(self._signals)
        return l

    def __str__(self):
        return "Signals of CST #%d, month %d-%d, contains %d signals" % (self.cst_id, self.year, self.month, self.get_signals_qnt())
